import javax.print.attribute.standard.OutputDeviceAssigned;
import java.io.Console;
import java.lang.System;
import java.math.BigInteger;
import java.sql.*;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
import java.util.Random;
import java.security.*;
import java.math.*;
/**
 * @(#)mysql_db_stress_test.java
 *
 *
 * @author
 * @version 1.00 2011/3/6
 */

public class mysql_db_stress_test {
	static Connection conn;
    static Statement s;

	public static void createdb() throws ClassNotFoundException,SQLException
	{
        mysql_db_stress_test.s.executeUpdate("CREATE DATABASE tmp123");
	}
	public static void createtable() throws ClassNotFoundException,SQLException
	{
        mysql_db_stress_test.s.executeUpdate("USE tmp123");
        mysql_db_stress_test.s.executeUpdate("CREATE TABLE usertest (`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,`username` CHAR(32) NOT NULL,`passwd` CHAR(32) NOT NULL,`email` CHAR(100) NOT NULL,`hash` CHAR(32) NOT NULL,PRIMARY KEY (`id`)  )");
	}
	public static void insertrows() throws ClassNotFoundException,SQLException,NoSuchAlgorithmException
	{
        String strpass;
        int randnum;
        Random rnd = new Random();
        String hash;
        MessageDigest m = MessageDigest.getInstance("MD5");
        byte[] data;
        BigInteger bi;
        String idx;
        for (int i = 1; i < 90001; i++)
        {
            idx = String.valueOf(i);
            randnum = rnd.nextInt(999 - 111 + 1) + 111;
            strpass = String.valueOf(randnum);
            data = ("example"+idx+strpass+"example"+idx+"@example.com").getBytes();
            m.update(data);
            bi = new BigInteger(1,m.digest());
            hash = String.format("%1$032X", bi);
            mysql_db_stress_test.s.executeUpdate("INSERT INTO usertest (`id`,`username`,`passwd`,`email`,`hash`) VALUES ( NULL,'example"+idx+"','"+strpass+"','example"+idx+"@example.com','"+hash+"')");
        }
	}
	public static void selectrows() throws ClassNotFoundException,SQLException,NoSuchAlgorithmException
	{
        mysql_db_stress_test.s.executeQuery("SELECT * FROM usertest");
        ResultSet rs = s.getResultSet();
        MessageDigest m = MessageDigest.getInstance("MD5");
        byte[] data;
        String datastr;
        BigInteger bi;
        String hash;
        while(rs.next())
        {
            datastr = rs.getString("username")+rs.getString("passwd")+rs.getString("email");
            m.update(datastr.getBytes());
            bi = new BigInteger(1,m.digest());
            hash = String.format("%1$032X", bi);
            if (!hash.equals(rs.getString("hash")))
                System.out.println("Something went wrong!");
        }
	}
	public static void deleterows() throws ClassNotFoundException,SQLException
	{
        mysql_db_stress_test.s.executeUpdate("DELETE FROM usertest");
	}
	public static void deletetable() throws ClassNotFoundException,SQLException
	{
        mysql_db_stress_test.s.executeUpdate("DROP TABLE usertest");
	}
	public static void deletedb() throws ClassNotFoundException,SQLException
	{
        mysql_db_stress_test.s.executeUpdate("DROP DATABASE tmp123");
        mysql_db_stress_test.s.close();
        mysql_db_stress_test.conn.close();
	}
	public static void main(String[] args)  throws ClassNotFoundException,SQLException,InstantiationException,IllegalAccessException,NoSuchAlgorithmException
    {
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		Properties p = new Properties();
		p.put("user", "root");
		p.put("password", "root");
		mysql_db_stress_test.conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/",p);
        mysql_db_stress_test.s = mysql_db_stress_test.conn.createStatement();
        mysql_db_stress_test.createdb();
        mysql_db_stress_test.createtable();
        mysql_db_stress_test.insertrows();
        mysql_db_stress_test.selectrows();
        mysql_db_stress_test.deleterows();
        mysql_db_stress_test.deletetable();
        mysql_db_stress_test.deletedb();
	}
}
